package com.yimixian.app.rest.api;

import com.yimixian.app.common.Constants;
import com.yimixian.app.model.Address;

import org.json.JSONObject;

import retrofit.Callback;
import retrofit.client.Response;

/**
 * Created by zhilian-2 on 2015/10/9.
 */
public class AddressAPI extends YmxBaseAPI<Address> {

    public static final String ADDRESSURL = "/v3/users/current/addresses/find_by_store_id";
    private int storeId;
    private String token = "";

    public AddressAPI(String token, int storeId, Callback<Response> cb){
        super(cb);
        this.storeId = storeId;
        this.token = token;
    }

    @Override
    public String getRelativeURL() {
        return ADDRESSURL;
    }

    public void getAddress(){
        mYmxRestService.getAddress(token, storeId, Constants.BAIDU_COORD_SYSTEM, mCallback);
    }

    @Override
    public Address parseJson(JSONObject jsonObject) {
        Address mAddress = null;
        if (jsonObject != null){
            JSONObject object = jsonObject.optJSONObject("address");
            if (object != null){
                mAddress = new Address();
                mAddress.province = object.optString("province");
                mAddress.provinceKey = object.optString("province_id");
                mAddress.city = object.optString("city");
                mAddress.cityKey = object.optString("city_id");
                mAddress.district = object.optString("district");
                mAddress.districtKey = object.optString("district_id");
                mAddress.coordSystem = object.optString("coord_system");
                mAddress.gender = object.optString("gender");
                mAddress.id = object.optInt("id");
                mAddress.isPickup = object.optBoolean("is_pickup");
                mAddress.latitude = object.optString("latitude");
                mAddress.longitude = object.optString("longitude");
                mAddress.name = object.optString("name");
                mAddress.poiAddress = object.optString("poi_address");
                mAddress.poiId = object.optString("poi_id");
                mAddress.poiName = object.optString("poi_name");
                mAddress.roomNo = object.optString("room_no");
                mAddress.tag = object.optString("tag");
                mAddress.tel = object.optString("tel");
                mAddress.storeId = object.optString("store_id");
            }

        }
        return mAddress;
    }
}
